package inet.app.view.activity;

import inet.app.R;
import inet.app.common.XoSoConstants;
import inet.app.controller.connect.JsonParser;
import inet.app.controller.parser.Parser_Kqxs;
import inet.app.controller.request.Request;
import inet.app.model.lottery.GetLoto;
import inet.app.model.lottery.LotteryResult;
import inet.app.model.object.KqGiai_Item;
import inet.app.model.util.DateProc;
import inet.app.model.util.ProgressDialog;
import inet.app.model.util.Util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/*public class LichMoThuongDetail extends BaseActivity implements
 OnClickListener, OnCompletionListener, OnTouchListener,
 OnPreparedListener */
public class LichMoThuongDetail extends BaseFragmentActivity implements
		OnClickListener, OnCompletionListener, OnTouchListener,
		OnPreparedListener {
	Button btthongke;
	String nhamang = "";
	String cuoc = " (Cước 1000đ)";
	private WebView webView;
	int start;
	ImageButton imgPlay;
	String kqDe = "";
	private TextView tvTitle;
	TextView tvDate;
	Dialog dialog;
	private String rngay = null, rthang = null, rgiaidb = null, rgiai1 = null,
			rgiai2 = null, rgiai3 = null, rgiai4 = null, rgiai5 = null,
			rgiai6 = null, rgiai7 = null, rgiai8 = null;
	boolean dangnghe = false;
	int myYear, myMonth, myDay, myHour, myMinute;
	private ProgressDialog pdia;
	private ArrayList<String> listaudio = new ArrayList<String>();
	private int position = 0;
	private MediaPlayer media;
	private AssetManager assets;
	private boolean isRunning = false;
	private String region;
	private Button bt_ThongKe;
	private String name, code;
	private Request request = new Request();
	private Parser_Kqxs parser = new Parser_Kqxs();
	ArrayList<KqGiai_Item> arraylist = new ArrayList<KqGiai_Item>();
	private String mangKQ, mangLo;
	private String[] listKQ;
	LotteryResult lotteryresult;
	int countclick = 0;
	String data[];
	String databuf[];
	String dau0, dau1, dau2, dau3, dau4, dau5, dau6, dau7, dau8, dau9;
	String duoi0, duoi1, duoi2, duoi3, duoi4, duoi5, duoi6, duoi7, duoi8,
			duoi9;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ketqua_mothuong);
		// setContentView(R.layout.ketqua_detail);
		pdia = new ProgressDialog(LichMoThuongDetail.this);
		initBase();
		getdata();
		setup();
		// Gét nhà mạng
		nhamang = Util.getSharePreferences(this);

	}

	private void getdata() {
		// TODO Auto-generated method stub
		Bundle rb = this.getIntent().getExtras();
		name = rb.getString("name");
		code = rb.getString("code");
		region = rb.getString("REGION");
	}

	private void setup() {
		// TODO Auto-generated method stub

		btthongke = (Button) findViewById(R.id.bt_LichMoThuong);
		btthongke.setText("Thống Kê");
		btthongke.setVisibility(View.VISIBLE);
		btthongke.setOnClickListener(this);

		tvDate = (TextView) findViewById(R.id.tvDate);
		bt_ThongKe = (Button) findViewById(R.id.bt_ThongKe);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		tvTitle.setText(" Kết Quả " + name);

		webView = (WebView) findViewById(R.id.webView);

		imgPlay = (ImageButton) findViewById(R.id.imgPlay);
		imgPlay.setOnClickListener(this);
		imgPlay.setBackgroundResource(R.drawable.button_on_sound);
		// Setup Ontouch
		tvDate.setOnTouchListener(this);
		// Set day, month, year
		Time time = new Time();
		time.setToNow();
		final Calendar cal = Calendar.getInstance();
		myYear = cal.get(Calendar.YEAR);
		myMonth = cal.get(Calendar.MONTH) + 1;
		myDay = cal.get(Calendar.DATE);
		// SET DATE CURRENT
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat df2 = new SimpleDateFormat("HH:mm");
		date_current = df.format(c.getTime());
		String timecurren = df2.format(c.getTime());
		// END SET TIME
		tvDate.setText(" Ngày " + date_current + "      ");

		webView.setWebViewClient(new MyWebViewClient());
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setPluginsEnabled(true);
		webView.getSettings().setSupportMultipleWindows(true);
		webView.getSettings().setSupportZoom(false);
		webView.setVerticalScrollBarEnabled(true);
		webView.setHorizontalScrollBarEnabled(false);
		// ---------------------------------SEND REQUEST-----------------------
		sendrequest(code, date_current, date_current);

		// -------------------------------SET UP
		// MEDIAPLAYER--------------------------//
		media = new MediaPlayer();
		media.setOnPreparedListener(this);
		media.setOnCompletionListener(this);
		assets = this.getAssets();
	}

	private void sendrequest(final String code, final String sdate,
			final String edate) {
		pdia.show();
		AsyncTask<Void, Integer, Void> myTask = new AsyncTask<Void, Integer, Void>() {
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				arraylist = parser.getList_Mt(request.getkqxs(code, sdate,
						edate));
				return null;
			}

			protected void onPostExecute(Void result) {
				super.onPreExecute();
				if (arraylist != null) {
					imgPlay.setEnabled(true);
					pdia.dismiss();
					Load_data_webview(arraylist);
					btthongke.setVisibility(View.VISIBLE);
				} else {
					imgPlay.setEnabled(false);
					btthongke.setVisibility(View.GONE);
					pdia.dismiss();
					webView.clearCache(true);
					webView.loadDataWithBaseURL(null, "", "text/html", "utf-8",
							null);
					Util.showDialog(1, LichMoThuongDetail.this);
				}
			}
		};
		myTask.execute();
	}

	private void sendrequest_touch(final String code, final String sdate,
			final String edate) {
		pdia.show();
		AsyncTask<Void, Integer, Void> myTask = new AsyncTask<Void, Integer, Void>() {
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				arraylist = parser.getList_Mt(request.getkqxs(code, sdate,
						edate));
				return null;
			}

			protected void onPostExecute(Void result) {
				super.onPreExecute();
				if (arraylist != null) {
					String date_result = arraylist.get(0).getDate();
					if (date_result.equals(sdate)) {
						pdia.dismiss();
						Load_data_webview(arraylist);
						imgPlay.setEnabled(true);
						btthongke.setVisibility(View.VISIBLE);
					} else {
						pdia.dismiss();
						webView.clearCache(true);
						imgPlay.setEnabled(false);
						btthongke.setVisibility(View.GONE);
						webView.loadDataWithBaseURL(null, "", "text/html",
								"utf-8", null);
						Util.showDialog(2, LichMoThuongDetail.this);
					}
				} else {
					pdia.dismiss();
					btthongke.setVisibility(View.GONE);
					webView.clearCache(true);
					webView.loadDataWithBaseURL(null, "", "text/html", "utf-8",
							null);
					Util.showDialog(1, LichMoThuongDetail.this);
				}
			}
		};
		myTask.execute();
	}

	public void Load_data_webview(ArrayList<KqGiai_Item> arraylist) {
		if (arraylist.get(0).getCode().equals("XSMB"))
			cuoc = "(Cước 1000đ)";
		else
			cuoc = "(Cước 2000đ)";
		dau0 = "";
		dau1 = "";
		dau2 = "";
		dau3 = "";
		dau4 = "";
		dau5 = "";
		dau6 = "";
		dau7 = "";
		dau8 = "";
		dau9 = "";
		duoi0 = "";
		duoi1 = "";
		duoi2 = "";
		duoi3 = "";
		duoi4 = "";
		duoi5 = "";
		duoi6 = "";
		duoi7 = "";
		duoi8 = "";
		duoi9 = "";
		// Set data on datepicker
		String ngay = arraylist.get(0).getDate();
		tvDate.setText(" Ngày " + ngay + "   ");
		myDay = Integer.parseInt(ngay.split("/")[0]);
		myMonth = Integer.parseInt(ngay.split("/")[1]);
		myYear = Integer.parseInt(ngay.split("/")[2]);
		// end set
		String giaidb = arraylist.get(0).getGiaidb();
		// ĐỀ
		kqDe = giaidb.substring(giaidb.length() - 2, giaidb.length());

		String giai1 = arraylist.get(0).getGiai1();
		String giai2 = arraylist.get(0).getGiai2();
		String[] giai3 = arraylist.get(0).getGiai3().split("-");
		String[] giai4 = arraylist.get(0).getGiai4().split("-");
		String giai5 = arraylist.get(0).getGiai5();
		String[] giai6 = arraylist.get(0).getGiai6().split("-");
		String giai7 = arraylist.get(0).getGiai7();
		String giai8 = arraylist.get(0).getGiai8();

		// ---------------------------------- Parser DAU
		// DUOI-----------------------------------//
		lotteryresult = new LotteryResult(giaidb, giai1, giai2, arraylist
				.get(0).getGiai3(), arraylist.get(0).getGiai4(), giai5,
				arraylist.get(0).getGiai6(), giai7, giai8);

		GetLoto getloto = new GetLoto();
		String numberfirts, numberend;
		Collection<String> cEndNumbers = getloto.getEndNumberOfLotteryResult(
				lotteryresult, 2);
		String endNumber = "";
		for (Iterator<String> it = cEndNumbers.iterator(); it.hasNext();) {
			endNumber = it.next();
			numberfirts = Character.toString(endNumber.charAt(0));
			numberend = Character.toString(endNumber.charAt(1));
			// ĐẦU
			if (numberfirts.equals("0") == true)
				dau0 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("1") == true)
				dau1 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("2") == true)
				dau2 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("3") == true)
				dau3 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("4") == true)
				dau4 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("5") == true)
				dau5 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("6") == true)
				dau6 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("7") == true)
				dau7 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("8") == true)
				dau8 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("9") == true)
				dau9 += endNumber.substring(1, 2) + " ";
			// ĐUÔI
			if (numberend.equals("0") == true)
				duoi0 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("1") == true)
				duoi1 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("2") == true)
				duoi2 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("3") == true)
				duoi3 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("4") == true)
				duoi4 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("5") == true)
				duoi5 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("6") == true)
				duoi6 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("7") == true)
				duoi7 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("8") == true)
				duoi8 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("9") == true)
				duoi9 += endNumber.substring(0, 1) + " ";
		}

		// Clear the cache

		webView.clearCache(true);
		String htmlKetqua = "", htmlfooter = "";
		int dauDe = 0, duoiDe = 0;
		if (!kqDe.contains("..") && kqDe != null) {
			dauDe = Integer.parseInt(kqDe.substring(0, 1));
			duoiDe = Integer.parseInt(kqDe.substring(1));
		}
		switch (dauDe) {
		case 0:
			dau0 = dau0.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 1:
			dau1 = dau1.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 2:
			dau2 = dau2.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 3:
			dau3 = dau3.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 4:
			dau4 = dau4.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 5:
			dau5 = dau5.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 6:
			dau6 = dau6.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 7:
			dau7 = dau7.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 8:
			dau8 = dau8.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 9:
			dau9 = dau9.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;

		default:
			break;
		}

		switch (duoiDe) {
		case 0:
			duoi0 = duoi0.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 1:
			duoi1 = duoi1.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 2:
			duoi2 = dau2.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 3:
			duoi3 = duoi3.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 4:
			duoi4 = duoi4.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 5:
			duoi5 = duoi5.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 6:
			duoi6 = duoi6.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 7:
			duoi7 = duoi7.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 8:
			duoi8 = duoi8.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 9:
			duoi9 = duoi9.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;

		default:
			break;
		}
		htmlKetqua += "<div  style=\"cursor: pointer;background-color:#323232;width: 100%\">"
				+ "<input style=\"background-color:#323232; color:#ffffff;; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
				+ "CLICK NHẬN KQXS "
				+ arraylist.get(0).getName()
				+ cuoc
				+ "\" onclick=\"window.location.href='"
				+ arraylist.get(0).getCode()
				+ "'\" >"
				+ "</div>"
				+ "<div><table width=\"100%\" border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\"style=\"text-align:center;\" cellpadding=\"0\">"
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g8.png\" width=\"35\" height=\"35\"/>"
				+ "<td class=\"right\" colspan=\"12\">"
				+ arraylist.get(0).getGiai8()
				+ "</td> </tr> "
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g7.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" colspan=\"12\">"
				+ arraylist.get(0).getGiai7()
				+ "</td> </tr> "
				+ "<tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g6.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" width=\"35%\" colspan=\"4\">"
				+ giai6[0]
				+ "</td> <td class=\"right\" width=\"35%\"colspan=\"4\">"
				+ giai6[1]
				+ "</td> <td class=\"right\" width=\"35%\"colspan=\"4\">"
				+ giai6[2]
				+ "</td> </tr> "
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g5.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" colspan=\"12\">"
				+ arraylist.get(0).getGiai5()
				+ "</td> </tr>"
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\" rowspan=\"2\">"
				+ "<img src=\"file:///android_res/drawable/img_giai4.png\" width=\"35\" height=\"70\"/>"
				+ "</td> <td class=\"right\" width=\"25%\"colspan=\"3\">"
				+ giai4[0]
				+ "</td> <td class=\"right\" colspan=\"3\" width=\"25%\">"
				+ giai4[1]
				+ "</td> <td class=\"right\" colspan=\"3\" width=\"25%\">"
				+ giai4[2]
				+ "</td> <td class=\"right\" colspan=\"3\" width=\"25%\">"
				+ giai4[3]
				+ "</td> </tr> "
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"right\" colspan=\"4\" width=\"30%\">"
				+ giai4[4]
				+ "</td> <td class=\"right\" colspan=\"4\" width=\"30%\">"
				+ giai4[5]
				+ "</td> <td class=\"right\" colspan=\"4\" width=\"30%\">"
				+ giai4[6]
				+ "</td> </tr> "
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g3.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" colspan=\"6\" width=\"50%\">"
				+ giai3[0]
				+ "</td> <td class=\"right\" colspan=\"6\" width=\"50%\">"
				+ giai3[1]
				+ "</td> </tr>"
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g2.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" colspan=\"12\">"
				+ arraylist.get(0).getGiai2()
				+ "</td> </tr> "
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_g1.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" colspan=\"12\">"
				+ arraylist.get(0).getGiai1()
				+ "</td> </tr> "
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\">"
				+ "<img src=\"file:///android_res/drawable/img_db.png\" width=\"35\" height=\"35\"/>"
				+ "</td> <td class=\"right\" style=\"color:red;\"colspan=\"12\">"
				+ arraylist.get(0).getGiaidb()
				+ "</td> </tr>"
				+ "</table></div>";

		// -----------------------------------------DAU
		// DUOI-------------------------------------//
		String htmldauduoi = "";
		htmldauduoi += "<div>"
				+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
				+ "<tr>"
				+ "<td width=\"50%\" valign=\"top\" colspan=\"12\">"
				+

				"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
				+

				"<tr bgcolor=\"#323232\" >"
				+ "<td width=\"10%\" height=\"35\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Đầu</td>"
				+ "<td width=\"90%\" height=\"35\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Lô Tô</td>"
				+ "</tr>"
				+

				"<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 0 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g0.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau0
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 1 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g1.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau1
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 2 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g2.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau2
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 3 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g3.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau3
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 4 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g4.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau4
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 5 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g5.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau5
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 6 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g6.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau6
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 7 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g7.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau7
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 8 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g8.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau8
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 9 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g9.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ dau9
				+ "</td>"
				+ "</tr>"
				+

				"</table>"
				+

				"</td>"

				+

				"<td width=\"50%\" valign=\"top\" colspan=\"12\">"
				+

				"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\" >"
				+

				"<tr bgcolor=\"#323232\">"
				+ "<td width=\"10%\" height=\"35\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Đít</td>"
				+ "<td width=\"90%\" height=\"35\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Lô Tô</td>"
				+ "</tr>"
				+

				"<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 0 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g0.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi0
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 1 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g1.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi1
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 2 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g2.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi2
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 3 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g3.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi3
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 4 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g4.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi4
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 5 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g5.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi5
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 6 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g6.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi6
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 7 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g7.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi7
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 8 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g8.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi8
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 9 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g9.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;padding-left:5px;\" colspan=\"12\" >"
				+ duoi9 + "</td>" + "</tr>" + "</table>" +

				"</td>" +

				"</tr>" + "</table>" + "</div>";
		htmlfooter = "<div style=\"margin:3px 0 0 0; background-color:#323232; height:35px; text-align:center;\">"
				+ "<span style=\"background-color:#323232; text-align:center; color:#ffffff; font-size:13px; position:relative; top:10px\" class=\"fontsize12\">"
				+ "<strong>"
				+ "Chơi xổ số kiến thiết ích nước lợi nhà !"
				+ "</strong></span>" + "</div>";
		webView.loadDataWithBaseURL("file:///android_asset/",XoSoConstants.css+
				htmlKetqua + htmldauduoi + htmlfooter+"</body></html>", "text/html", "utf-8",
				null);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Bundle rb = new Bundle();
		switch (v.getId()) {
		case R.id.imgPlay:
			countclick++;
			if (media != null) {
				if (media.isPlaying()) {
					dangnghe = false;
					media.pause();
					start = 0;
					imgPlay.setBackgroundResource(R.drawable.button_on_sound);
				} else {
					start = 1;
					dangnghe = true;
					media.start();
					imgPlay.setBackgroundResource(R.drawable.button_off_sound);
				}
			}
			break;
		case R.id.btPlay:
			System.out.println("Play......");
			break;
		case R.id.btStop:
			System.out.println("Stop......");
			break;
		case R.id.bt_LichMoThuong:
			position = 0;
			media.stop();
			pdia.show();

			final Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg.what == 0) {
						String json = (String) msg.obj;
						Intent intent_app = new Intent(LichMoThuongDetail.this,
								ThongKeFragment.class);
						Bundle rb = new Bundle();
						rb.putString("json", json);
						intent_app.putExtras(rb);
						pdia.dismiss();
						startActivity(intent_app);
					} else if (msg.what == 1) {
						pdia.dismiss();
						Util.showDialog(1, LichMoThuongDetail.this);
					}
				}

			};
			Thread thread = new Thread() {
				public void run() {
					JsonParser jParser = new JsonParser();
					String jobject = jParser.getURL(request.gettk("XSTD", "10",
							"null"));
					if (jobject != null) {
						Message msg = handler.obtainMessage(0, jobject);
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage(1);
						handler.sendMessage(msg);
					}

				}
			};

			thread.start();
			break;

		}
	}

	private void addAudio() {
		// listaudio.clear();
		if (arraylist != null) {

			for (int k = 0; k < arraylist.size(); k++) {
				listaudio.clear();
				System.out.println("NGAY : " + arraylist.get(k).getDate());
				System.out.println("giaidb : " + arraylist.get(k).getGiai1());
				System.out.println("giai1 : " + arraylist.get(k).getGiai2());
				System.out.println("giai2 : " + arraylist.get(k).getGiai3());
				System.out.println("giai3 : " + arraylist.get(k).getGiai4());
				System.out.println("giai4 : " + arraylist.get(k).getGiai5());
				System.out.println("giai5 : " + arraylist.get(k).getGiai6());

				rngay = arraylist.get(k).getDate().split("/")[0];
				rthang = arraylist.get(k).getDate().split("/")[1];
				rgiaidb = arraylist.get(k).getGiaidb();
				rgiai1 = arraylist.get(k).getGiai1();
				rgiai2 = arraylist.get(k).getGiai2();
				rgiai3 = arraylist.get(k).getGiai3();
				rgiai4 = arraylist.get(k).getGiai4();
				rgiai5 = arraylist.get(k).getGiai5();
				rgiai6 = arraylist.get(k).getGiai6();
				rgiai7 = arraylist.get(k).getGiai7();
				rgiai8 = arraylist.get(k).getGiai8();

				listaudio.add("voice/Xoso/Xs_kqxs.mp3");
				listaudio.add(getProvince(arraylist.get(k).getCode()));
				listaudio.add("voice/Xoso/Xs_mothuong.mp3");
				listaudio.add(getDay(rngay));
				listaudio.add(getMonth(rthang));

				// giai 8
				listaudio.add("voice/Giai/Giai_8.mp3");
				data = rgiai8.split("");
				for (int i = 1; i < data.length; i++) {
					listaudio.add(getNumber(data[i]));
				}
				listaudio.add("voice/Beep/beep-2.mp3");

				if (!rgiai8.toString().contains(".....")
						&& rgiai8.toString().length() > 1) {
					// giai 7
					listaudio.add("voice/Giai/Giai_7.mp3");
					data = rgiai7.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}

				if (!rgiai7.toString().contains(".....")
						&& rgiai7.toString().length() > 1) {
					// giai6
					listaudio.add("voice/Giai/Giai_6.mp3");
					databuf = rgiai6.split("-");
					for (int j = 0; j < databuf.length; j++) {
						data = databuf[j].split("");
						for (int i = 1; i < data.length; i++) {
							listaudio.add(getNumber(data[i]));
						}
						listaudio.add("voice/Beep/beep-2.mp3");
					}
				}
				System.out.println("LENG G6  " + rgiai6.split("-").length);
				if (rgiai6.split("-").length >= 3) {
					// giai 5
					listaudio.add("voice/Giai/Giai_5.mp3");
					data = rgiai5.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
				if (!rgiai5.toString().contains(".....")
						&& rgiai5.toString().length() > 1) {
					// giai4
					listaudio.add("voice/Giai/Giai_4.mp3");
					databuf = rgiai4.split("-");
					for (int j = 0; j < databuf.length; j++) {
						data = databuf[j].split("");
						for (int i = 1; i < data.length; i++) {
							listaudio.add(getNumber(data[i]));
						}
						listaudio.add("voice/Beep/beep-2.mp3");
					}
				}
				if (rgiai4.split("-").length >= 7) {
					// giai3
					listaudio.add("voice/Giai/Giai_3.mp3");
					databuf = rgiai3.split("-");
					for (int j = 0; j < databuf.length; j++) {
						data = databuf[j].split("");
						for (int i = 1; i < data.length; i++) {
							listaudio.add(getNumber(data[i]));
						}
						listaudio.add("voice/Beep/beep-2.mp3");
					}
				}
				if (rgiai3.split("-").length >= 2) {
					// giai2
					listaudio.add("voice/Giai/Giai_2.mp3");
					data = rgiai2.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
				if (!rgiai2.toString().contains(".....")
						&& rgiai2.toString().length() > 1) {
					// giai1
					listaudio.add("voice/Giai/Giai_1.mp3");
					data = rgiai1.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
				if (!rgiai1.toString().contains(".....")
						&& rgiai1.toString().length() > 1) {
					// giai db
					listaudio.add("voice/Giai/Giai_Db.mp3");
					data = rgiaidb.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}
		}
	}

	private String getProvince(String id) {
		return "voice/TenTinh/Tinh_" + id + ".mp3";
	}

	private String getDay(String day) {
		return "voice/Ngay/Ngay_" + day + ".mp3";
	}

	private String getMonth(String month) {
		return "voice/Thang/Thang_" + month + ".mp3";
	}

	private String getNumber(String number) {
		return "voice/So/So_" + number + ".mp3";
	}

	private String getGiai(int giai) {
		return "voice/Giai/Giai_" + giai + ".mp3";
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		Time time = new Time();
		time.setToNow();
		if (position <= 112) {
			try {
				Thread.sleep(400);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mp.stop();
			if (dangnghe)
				media.reset();
			if (arraylist != null)
				PlayVideo();
			System.out.println("POSITION LA MT=================>> " + position);
		} else {
			media.stop();
			position = 0;
			imgPlay.setBackgroundResource(R.drawable.button_on_sound);
		}
	}

	private void PlayVideo() {
		try {
			addAudio();
			if (rgiaidb != null
					&& listaudio.size() >= position
					&& listaudio.size() > position
					&& (rgiaidb != null && rgiai1 != null && rgiai2 != null
							&& rgiai3 != null && rgiai4 != null
							&& rgiai5 != null && rgiai6 != null
							&& rgiai7 != null && rgiai8 != null)) {
				AssetFileDescriptor fd = assets.openFd(listaudio.get(position));
				media.setDataSource(fd.getFileDescriptor(),
						fd.getStartOffset(), fd.getLength());
				media.setAudioStreamType(AudioManager.STREAM_RING);
				media.prepare();
				position++;
			}

		} catch (IllegalArgumentException e) {
			System.out.println("Error while playing video");
			e.printStackTrace();
		} catch (IllegalStateException e) {
			System.out.println("Error while playing video");
			e.printStackTrace();
		} catch (IOException e) {
			System.out
					.println("Error while playing video. Please, check your network connection.");
			e.printStackTrace();
		}
	}

	// Dialog Loading
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN)
			switch (v.getId()) {
			case R.id.tvDate:
				DatePickerDialog.OnDateSetListener myOnDateListener = new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						// TODO Auto-generated method stub
						media.reset();
						position = 0;
						dangnghe = false;
						imgPlay.setBackgroundResource(R.drawable.button_on_sound);

						myYear = year;
						myMonth = monthOfYear + 1;
						myDay = dayOfMonth;
						// FORMAT DATE
						SimpleDateFormat form1 = new SimpleDateFormat(
								"dd/MM/yyyy");
						java.sql.Timestamp ts1 = DateProc.buildTimestamp(
								myYear, myMonth, myDay);
						date_current = form1.format(ts1);
						// END
						tvDate.setText(" Ngày " + date_current + "      ");
						sendrequest_touch(code, date_current, date_current);
					}
				};
				DatePickerDialog datepicker = new DatePickerDialog(this,
						myOnDateListener, myYear, myMonth - 1, myDay);
				datepicker.show();
				break;
			default:
				break;
			}
		return false;
	}

	public void onResume() {
		// TODO Auto-generated method stub
		if (((MyApp) getApplication()).isKill()) {
			finish();
		} else {
			((MyApp) getApplication()).setRunning(true);
		}
		super.onResume();
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub
		if (countclick >= 1) {
			if (!media.isPlaying()) {
				media.start();
			}
		}
	}

	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (!nhamang.contains("viettel")) {
				Util.sendSMS("DK XSMB", "8985", LichMoThuongDetail.this);
			} else {
				Util.sendSMS("XSMB", "8185", LichMoThuongDetail.this);
			}
			Toast.makeText(LichMoThuongDetail.this, "Đã đăng ký nhận kết quả!",
					Toast.LENGTH_LONG).show();
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			// page could not be loaded
		}
	}

}